﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class Them_ChinhSuaNhaCungUng : Form
    {
        public string MSNCU;

        private void clearInformation()
        {
            textBoxMaSo_NCU.Text = "";
            textBoxDiaChi_NCU.Text = "";
            textBoxSDT_NCU.Text = "";
            textBoxFax_NCU.Text = "";
            textBoxEmail_NCU.Text = "";
        }

        private void clearNotification()
        {
            labelEmail_NCUError.Text = "";
            labelMaSo_NCUError.Text = "";
        }

        private void insert_NhaCungUng(string MaSo_NCU, string DiaChi_NCU, string SDT_NCU, string Fax_NCU, string Email_NCU)
        {
            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhaCungUng where MaSo_NCU = @MaSo_NCU or Email_NCU = @Email_NCU";
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.AddWithValue("@MaSo_NCU", MaSo_NCU);
            cmd.Parameters.AddWithValue("@Email_NCU", Email_NCU);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                if (reader["MaSo_NCU"].ToString() == MaSo_NCU)
                {
                    labelMaSo_NCUError.Text = "Mã số nhà cung ứng này đã tồn tại!";
                    labelThongBaoThemNCU.Text = "Bạn đã thêm vào không thành công!";
                }
                else
                {
                    labelEmail_NCUError.Text = "Email này đã tồn tại!";
                    labelThongBaoThemNCU.Text = "Bạn đã thêm vào không thành công!";
                }
            }
            else
            {
                reader.Close();
                cmd.CommandText = "insert into NhaCungUng(MaSo_NCU, DiaChi_NCU, SDT_NCU, Fax_NCU, Email_NCU) values (@MaSo_NCU, @DiaChi_NCU, @SDT_NCU, @Fax_NCU, @Email_NCU)";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@MaSo_NCU", MaSo_NCU);
                cmd.Parameters.AddWithValue("@DiaChi_NCU", DiaChi_NCU);
                cmd.Parameters.AddWithValue("@SDT_NCU", SDT_NCU);
                cmd.Parameters.AddWithValue("@Fax_NCU", Fax_NCU);
                cmd.Parameters.AddWithValue("@Email_NCU", Email_NCU);
                cmd.ExecuteNonQuery();
                labelThongBaoThemNCU.Text = "Bạn đã thêm vào thành công!";
            }
            conn.Close();
        }

        public Them_ChinhSuaNhaCungUng()
        {
            InitializeComponent();
        }

        public Them_ChinhSuaNhaCungUng(string MSNCU)
        {
            InitializeComponent();
            this.MSNCU = MSNCU;
        }

        private void KhoaThongTin()
        {
            textBoxDiaChi_NCU.Enabled = false;
            textBoxSDT_NCU.Enabled = false;
            textBoxFax_NCU.Enabled = false;
            textBoxEmail_NCU.Enabled = false;

        }

        private void XoaThongTin()
        {
            textBoxMaSo_NCU.Text = "";
            textBoxDiaChi_NCU.Text = "";
            textBoxSDT_NCU.Text = "";
            textBoxFax_NCU.Text = "";
            textBoxEmail_NCU.Text = "";
        }

        private void LoadDataGridViewChinhSuaNhaCungUng()
        {
            //Config file = new Config(Application.StartupPath + "\\config.ini");
            //string connectionstring = file.ReadValue("server", "con");
            //SqlConnection conn = new SqlConnection(connectionstring);
            //string sql = "select * from NhaCungUng";
            //SqlCommand cmd = new SqlCommand(sql, conn);
            //conn.Open();
            //SqlDataReader reader = cmd.ExecuteReader();
            //while (reader.Read())
            //{
            //    dataGridViewSuaNCU.Rows.Add(new string[] { reader["MaSo_NCU"].ToString(), (string)reader["DiaChi_NCU"], (string)reader["SDT_NCU"], (string)reader["Fax_NCU"], (string)reader["Email_NCU"] });
            //}
            //reader.Close();
            //conn.Close();

            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhaCungUng";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "NhaCungUng");
            dataGridViewSuaNCU.AutoGenerateColumns = false;
            dataGridViewSuaNCU.DataSource = ds.Tables["NhaCungUng"];
            conn.Close();
        }

        private void LoadThongTinNCU(string MSNCU)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhaCungUng where MaSo_NCU = @MaSo_NCU";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_NCU", MSNCU);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                textBoxMaSo_NCU.Text = reader["MaSo_NCU"].ToString();
                textBoxDiaChi_NCU.Text = reader["DiaChi_NCU"].ToString();
                textBoxSDT_NCU.Text = reader["SDT_NCU"].ToString();
                textBoxFax_NCU.Text = reader["Fax_NCU"].ToString();
                textBoxEmail_NCU.Text = reader["Email_NCU"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void updateNCU(string MSNCU, string DCNCU, string SDTNCU, string FNCU, string ENCU)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update NhaCungUng set DiaChi_NCU = @DiaChi_NCU, SDT_NCU = @SDT_NCU, Fax_NCU = @Fax_NCU, Email_NCU = @Email_NCU where MaSo_NCU = @MaSo_NCU";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_NCU", MSNCU);
            cmd.Parameters.AddWithValue("@DiaChi_NCU", DCNCU);
            cmd.Parameters.AddWithValue("@SDT_NCU", SDTNCU);
            cmd.Parameters.AddWithValue("@Fax_NCU", FNCU);
            cmd.Parameters.AddWithValue("@Email_NCU", ENCU);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void deleteNCU(string MSNCU, string DCNCU, string SDTNCU, string FNCU, string ENCU)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "delete from NhaCungUng where MaSo_NCU = @MaSo_NCU";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_NCU", MSNCU);
            cmd.Parameters.AddWithValue("@DiaChi_NCU", DCNCU);
            cmd.Parameters.AddWithValue("@SDT_NCU", SDTNCU);
            cmd.Parameters.AddWithValue("@Fax_NCU", FNCU);
            cmd.Parameters.AddWithValue("@Email_NCU", ENCU);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void Them_ChinhSuaNhaCungUng_Load(object sender, EventArgs e)
        {
            LoadDataGridViewChinhSuaNhaCungUng();
        }

        private void ButtonChinhSuaNCU_Click(object sender, EventArgs e)
        {
            textBoxDiaChi_NCU.Enabled = true;
            textBoxSDT_NCU.Enabled = true;
            textBoxFax_NCU.Enabled = true;
            textBoxEmail_NCU.Enabled = true;

            LoadDataGridViewChinhSuaNhaCungUng();
        }

        private void ButtonCapNhatNCU_Click(object sender, EventArgs e)
        {

            ThongBaoThayDoi f = new ThongBaoThayDoi();
            if (f.ShowDialog() == DialogResult.OK)
            {
                updateNCU(textBoxMaSo_NCU.Text, textBoxDiaChi_NCU.Text, textBoxSDT_NCU.Text, textBoxFax_NCU.Text, textBoxEmail_NCU.Text);
                f.Close();
                XoaThongTin();
                LoadDataGridViewChinhSuaNhaCungUng();
                KhoaThongTin();
            }
            else
                f.Close();
        }

        private void ButtonXoaNCU_Click(object sender, EventArgs e)
        {
            ThongBaoXoa f = new ThongBaoXoa();
            if (f.ShowDialog() == DialogResult.OK)
            {
                deleteNCU(textBoxMaSo_NCU.Text, textBoxDiaChi_NCU.Text, textBoxSDT_NCU.Text, textBoxFax_NCU.Text, textBoxEmail_NCU.Text);
                LoadDataGridViewChinhSuaNhaCungUng();
            }
            else
                f.Close();
        }

        private void dataGridViewSuaNCU_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string MSNCU;
            MSNCU = dataGridViewSuaNCU.Rows[e.RowIndex].Cells["MaSo_NCU"].Value.ToString();
            LoadThongTinNCU(MSNCU);
        }

        private void ButttonThemNCU_Click(object sender, EventArgs e)
        {
            if (textBoxMaSo_NCU.Text == "" || textBoxDiaChi_NCU.Text == "" || textBoxSDT_NCU.Text == "" || textBoxFax_NCU.Text == "" || textBoxEmail_NCU.Text == "")
                labelThongBaoThemNCU.Text = "Xin hãy nhập đầy đủ dữ liệu!";
            else
            {
                if ((MessageBox.Show("Bạn có muốn thêm dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
                {
                    clearNotification();
                    insert_NhaCungUng(textBoxMaSo_NCU.Text, textBoxDiaChi_NCU.Text, textBoxSDT_NCU.Text, textBoxFax_NCU.Text, textBoxEmail_NCU.Text);
                    clearInformation();
                    LoadDataGridViewChinhSuaNhaCungUng();
                }
            }
        }

        private void ButtonThoatNCU_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
